package main

import (
	"github.com/giter/go.rice"
	"os"
	"github.com/urfave/cli"
	"strings"
	"gitee.com/dayu1985/can_api/controller/base"
	service "gitee.com/dayu1985/can_api/service/base"
	_ "gitee.com/dayu1985/can_api/controller"
	"gitee.com/dayu1985/can_api/controller/app"
	"gitee.com/dayu1985/can_api/utils"
)

func main() {

	//1. 文件打包
	os.Mkdir("resource", os.ModePerm)
	os.Mkdir("resource/doc", os.ModePerm)
	base.HTTPBox["doc"] = rice.MustFindBox("resource/doc").HTTPBox()
	base.Box["conf"] = rice.MustFindBox("conf")
	cbox := rice.MustFindBox("conf")
	app.Init(cbox)

	//2. 实例化midway代理
	comm := cli.NewApp()
	comm.Name = app.MustValue("system", "name")
	comm.Description = app.MustValue("system", "brief")
	comm.Version = app.MustValue("system", "version")
	comm.Action = func(c *cli.Context) error {
		return base.NewApplication()
	}
	comm.Commands = []cli.Command{
		{
			Name:  "sync",
			Usage: "初始化配置",
			Action: func(c *cli.Context) error {
				e := service.NewService()
				return e.Sync()
			},
		},
		{
			Name:  "zip",
			Usage: "获取生产环境下的zip包",
			Action: func(c *cli.Context) error {
				//获取源文件列表
				dirstr := app.MustValue("system", "deploy.dir", "conf/")
				dirs := strings.Split(dirstr, ",")

				files := make([]*os.File, 0)

				for _, dir := range dirs {
					f, err := os.Open(dir)
					if err == nil {
						files = append(files, f)
					}
				}

				err := utils.Compress(files, comm.Name+"-"+comm.Version+".zip", comm.Name)
				if err != nil {
					panic(err)
				}
				return nil
			},
		},
	}

	if err := comm.Run(os.Args); err != nil {
		panic(err.Error())
	}

}
